<?php

namespace Wgx\Admin\Entity;

use PhpYes\Apps;
use PhpYes\Entity\BaseEntity;
use Wgx\PdoEx\QueryBuilder;

class AdminMenuEntity extends BaseEntity
{
    const AdminMenuRootId = 1;

    /** @var array|null */
    private static $allMenuData;
    /** @var array|null */
    private static $allId2MenuMap;

    /** @var null|AdminMenuEntity */
    private $parentEntity = null;

    /**
     * @return array|null
     */
    public static function getAllMenuData()
    {
        if (empty(self::$allMenuData)) {
            $entityTpl = self::getSingle();
            $qb = QueryBuilder::useDb($entityTpl->getConnName());
            self::$allMenuData = $qb->select($entityTpl->getTable(), '*')->getPdoStatement()->fetchAll(\PDO::FETCH_ASSOC);
        }
        return self::$allMenuData;
    }

    /**
     * @return array|null
     */
    public static function getAllId2MenuMap()
    {
        if (empty(self::$allId2MenuMap)) {
            $allMenu = self::getAllMenuData();
            if ($allMenu) {
                self::$allId2MenuMap = array_column($allMenu, null, 'id');
            }
        }
        return self::$allId2MenuMap;
    }

    public function __construct($data = null)
    {
        if (empty($data)) {
            $data = array();
        }
        if (empty($data['level'])) {
            $data['level'] = 0;
        }
        if (empty($data['rights'])) {
            $data['rights'] = AdminToMenuEntity::RightRead;
        }
        if (empty($data['priority'])) {
            $data['priority'] = 0;
        }
        if (empty($data['name'])) {
            $data['name'] = '菜单';
        }
        if (empty($data['is_in_menubar'])) {
            $data['is_in_menubar'] = 0;
        }

        parent::__construct($data);
    }

    /**
     * @inheritDoc
     */
    public function getConnName()
    {
        return App::$p->adminToMenuDb()->getConnName();
    }

    /**
     * @inheritDoc
     */
    public function getDb($quoted = true)
    {
        return App::$p->adminToMenuDb()->getDb($quoted);
    }

    /**
     * @inheritDoc
     */
    public function getTable($quoted = true)
    {
        return 'admin_menu';
    }

    public function formatNormal()
    {
        return $this->format(array(
            'is_in_menubar' => 'isInMenubar',
            'icon_color' => 'iconColor',
            'default_query' => 'defaultQuery',
        ));
    }

    public function formatSimple()
    {
        return $this->format(array(
            'is_in_menubar' => 'isInMenubar',
            'priority' => false,
            'icon' => false,
            'icon_color' => false,
            'default_query' => false,
        ));
    }

    /**
     * @return int|null
     */
    public function getPid()
    {
        return $this->get_('pid');
    }

    /**
     * @return AdminMenuEntity|null
     */
    public function getParentEntity()
    {
        $pid = intval($this->get_('pid'));
        if ($this->parentEntity) {
            if ($pid) {
                if ($pid !== intval($this->parentEntity->getId())) {
                    $this->parentEntity = AdminMenuEntity::getSingle()->selectById($pid);
                }
            } else {
                $this->parentEntity = null;
            }
        } else {
            if ($pid) {
                $this->parentEntity = AdminMenuEntity::getSingle()->selectById($pid);
            } else {
                $this->parentEntity = null;
            }
        }

        return $this->parentEntity;
    }

    /**
     * @param AdminMenuEntity|null $parentEntity
     */
    public function setParentEntity($parentEntity)
    {
        if (empty($parentEntity)) {
            throw new \InvalidArgumentException('请传递parentEntity参数');
        }
        $newPid = intval($parentEntity->getId());
        if (empty($newPid)) {
            throw new \InvalidArgumentException('请传递有效的parentEntity参数');
        }

        $this->parentEntity = $parentEntity;
        $pid = intval($this->get_('pid'));
        if ($pid !== $newPid) {
            $this->set_('pid', $newPid, self::DataTypeInt);
            $this->set_('level', intval($parentEntity->getLevel()) + 1, self::DataTypeInt);
        }
    }

    /**
     * @return int
     */
    public function getLevel()
    {
        return $this->get_('level');
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->get_('name');
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->set_('name', $name, self::DataTypeString);
    }

    /**
     * @return string|null
     */
    public function getPath()
    {
        return $this->get_('path');
    }

    /**
     * @param string|null $path
     */
    public function setPath($path)
    {
        $this->set_('path', $path, self::DataTypeString, true);
    }

    /**
     * @return int
     */
    public function getRights()
    {
        return $this->get_('rights');
    }

    /**
     * @param int $rights
     */
    public function setRights($rights)
    {
        $this->set_('rights', $rights, self::DataTypeInt);
    }

    /**
     * @return bool
     */
    public function getIsInMenubar()
    {
        return $this->get_('is_in_menubar');
    }

    /**
     * @param bool $isInMenubar
     */
    public function setIsInMenubar($isInMenubar)
    {
        $this->set_('is_in_menubar', $isInMenubar ? 1 : 0, self::DataTypeBool);
    }

    /**
     * @return int
     */
    public function getPriority()
    {
        return $this->get_('priority');
    }

    /**
     * @param int $priority
     */
    public function setPriority($priority)
    {
        $this->set_('priority', $priority, self::DataTypeInt);
    }

    /**
     * @return string|null
     */
    public function getIcon()
    {
        return $this->get_('icon');
    }

    /**
     * @param string|null $icon
     */
    public function setIcon($icon)
    {
        $this->set_('icon', $icon, self::DataTypeString, true);
    }

    /**
     * @return string|null
     */
    public function getIconColor()
    {
        return $this->get_('icon_color');
    }

    /**
     * @param string|null $iconColor
     */
    public function setIconColor($iconColor)
    {
        $this->set_('icon_color', $iconColor, self::DataTypeString, true);
    }

    /**
     * @return string|null
     */
    public function getDefaultQuery()
    {
        return $this->get_('default_query');
    }

    /**
     * @param string|null $defaultQuery
     */
    public function setDefaultQuery($defaultQuery)
    {
        $this->set_('default_query', $defaultQuery, self::DataTypeString, true);
    }
}